﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Alex.Library.Web.Ajax
{
    /// <summary>
    /// XML序列化工具类
    /// </summary>
    class XmlSerializeHelper : SerializeHelper
    {
        public override bool IncludeNull { get; set; }

        /// <summary>
        /// 将对象序列化为XML字符串
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象</param>
        /// <param name="encoding">字符编码</param>
        /// <returns></returns>
        public override string Serialize<T>(T obj, Encoding encoding)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            try
            {
                serializer.Serialize(ms, obj);
                return encoding.GetString(ms.GetBuffer());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
                ms.Dispose();
            }
        }

        /// <summary>
        /// 将对象序列化为XML字符串
        /// </summary>        
        /// <param name="obj">对象</param>
        /// <param name="encoding">字符编码</param>
        /// <returns></returns>
        public override string Serialize(object obj, Encoding encoding)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            try
            {
                serializer.Serialize(ms, obj);
                return encoding.GetString(ms.GetBuffer());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
                ms.Dispose();
            }
        }

        /// <summary>
        /// 将XML字符串中反序列化为对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="serializeString">XML字符串</param>
        /// <param name="encoding">字符编码</param>
        /// <returns></returns>
        public override T Deserialize<T>(string serializeString, Encoding encoding)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            try
            {
                byte[] buffer = encoding.GetBytes(serializeString);
                ms.Write(buffer, 0, buffer.Length);
                return (T)serializer.Deserialize(ms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
                ms.Dispose();
            }
        }

        /// <summary>
        /// 将XML字符串中反序列化为对象
        /// </summary>
        /// <param name="serializeString">XML字符串</param>
        /// <param name="type">反序列化对象的类型</param>
        /// <param name="encoding">字符编码</param>
        /// <returns></returns>
        public override object Deserialize(string serializeString, Type type, Encoding encoding)
        {
            XmlSerializer serializer = new XmlSerializer(type);
            MemoryStream ms = new MemoryStream();
            try
            {
                byte[] buffer = encoding.GetBytes(serializeString);
                ms.Write(buffer, 0, buffer.Length);
                return serializer.Deserialize(ms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
                ms.Dispose();
            }
        }
    }
}
